package com.spring.gop.proj.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.spring.gop.proj.interfaces.LoginService;
import com.spring.gop.proj.interfaces.TicketService;
import com.spring.gop.proj.model.MyPoint;
import com.spring.gop.proj.model.User;

@Controller
@RequestMapping("/ticket/*")
@SessionAttributes("user")
public class TicketController {

	@Autowired private LoginService loginService;
	@Autowired private TicketService ticketService;
	
	@RequestMapping(value="/start", method=RequestMethod.GET)
	public void startForm() {
		
	}
	
	@RequestMapping(value="/start", method=RequestMethod.POST)
	public String start(@ModelAttribute("newMyPoint")MyPoint myPoint, HttpSession session) {
		
		User newUser = (User)session.getAttribute("user");
		ticketService.insertTicket(myPoint, newUser);
		
		session.setAttribute("user", newUser);
		
		String returnUrl = (String)session.getAttribute("returnUrl");
		session.removeAttribute("returnUrl");
		String targetUrl = (String)session.getAttribute("targetUrl");
		if(targetUrl != null) session.removeAttribute("targetUrl");
		
		if(returnUrl != null) {

			if(returnUrl.equals("/main") || returnUrl.equals("/movie/list")) return "redirect:start";
			else return "redirect:" + returnUrl;
		} else return "redirect:start";
	}
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public void listForm(Model model) {
		List<MyPoint> list = ticketService.selectAllMyPoint();
		int totalGain = 0;
		
		for(int i=0; i<list.size(); i++) {
			totalGain += list.get(i).getPoint_value();
		}
		
		model.addAttribute("list", list);
		model.addAttribute("totalGain", totalGain);
	}
}




























